#pragma once
#include <event2/event.h>
#include "linklist.h"
#define ZHTTP_MAX_LINE_SIZE           1024*2
#define ZHTTP_MAX_HEADER_SIZE   1024*4
#define ZHTTP_MAX_BODY_SIZE         1024*1024*6
enum zhttp_flag {
	ZHTTP_FLAG_LINE,
	ZHTTP_FLAG_HEAD,
	ZHTTP_FLAG_BODY,
	ZHTTP_FLAG_DONE
};
enum zhttp_method {
	EVHTTP_REQ_GET          = 1 << 0,
	EVHTTP_REQ_POST       = 1 << 1,
	EVHTTP_REQ_HEAD       = 1 << 2,
	EVHTTP_REQ_PUT          = 1 << 3,
	EVHTTP_REQ_DELETE    = 1 << 4,
	EVHTTP_REQ_OPTIONS = 1 << 5,
	EVHTTP_REQ_TRACE       = 1 << 6,
	EVHTTP_REQ_CONNECT = 1 << 7,
	EVHTTP_REQ_PATCH        = 1 << 8
};
struct RequestLine
{
    char method[12];
    char protocol[12];
    char* url;
};
struct rhkeyval
{
    LinkListNode node;
    char* key;
    char* value;
};
struct Request
{
    struct RequestLine* line;
    LinkList* header;
    char* body;
};
struct zhttp
{
    enum zhttp_flag  status;
    struct Request*    request;
    char* buf;

    struct event_base* base;
};
struct zhttp* zhttp_new(struct event_base* base);
void  zhttp_clear(struct zhttp* http);
struct Request* request_new();
int request_set_line(struct zhttp* http, char* p);
int request_set_head(struct zhttp* http, char* p);
void  request_free(struct Request* request);
void zhttp_free(struct zhttp* http);

void append_http_buf(struct zhttp* http, char* buf, int len);
void http_line_parse(struct zhttp* http, struct bufferevent *bev);
void http_header_parse(struct zhttp* http, struct bufferevent *bev);
void http_body_parse(struct zhttp* http, struct bufferevent *bev);
int http_allowed_method(char* method);


// void free_request_line(struct Request* request);
// void setRequestLine(struct Request* request, char* p);
// void setRequestHead(struct Request* request, char* p);